package com.edu.d8_lambda;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Comparator;

public class LambdaDemo2 {
    /**
     * lambda简化代码的案例
     */
    public static void main(String[] args) {
        Integer[] num2={13,44,55,36,73,47,55,99};

        //案例一：
        /**
         * 参数一：被降序的数组，必须是引用数据类型元素
         * 参数二：匿名内部类对象，代表了一个比较器对象
         */
        /*Arrays.sort(num2, new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {

                return -(integer-t1);
            }
        });*/
        //简化之后的
        /*Arrays.sort(num2,(t1,t2)->{
            return -(t1-t2);
        });*/
        Arrays.sort(num2 , (t1 , t2)-> t2-t1);
        System.out.println(Arrays.toString(num2));

        //案例二：
        //创建一个窗口格子
        JFrame frame = new JFrame();
        //创建一个桌布
        JPanel panel = new JPanel();
        //创建四个按钮对象
        JButton btn1 = new JButton("上");
        JButton btn2 = new JButton("下");
        JButton btn3 = new JButton("左");
        JButton btn4 = new JButton("右");
        //把按钮添加到桌布上
        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btn4);
        //把桌布添加上窗口
        frame.add(panel);
        //设置窗口格式
        frame.setLocationRelativeTo(null);
        frame.setSize(400,300);
        frame.setVisible(true);
        //设置四个监听机制
        /*btn1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Enum.UP);
            }
        });*/
        //简化之后
        //btn1.addActionListener((ActionEvent actionEvent)->{move(Enum.UP);});
        btn1.addActionListener((actionEvent)->{move(Enum.UP);});

/*        btn2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Enum.Down);
            }
        });*/
        //简化之后
        //btn2.addActionListener((ActionEvent actionEvent)->{move(Enum.Down);});
        btn2.addActionListener((actionEvent)->{move(Enum.Down);});
        /*btn3.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Enum.Left);
            }
        });*/
        //简化之后
        //btn3.addActionListener((ActionEvent actionEvent)->{move(Enum.Left);});
        btn3.addActionListener((actionEvent)->{move(Enum.Left);});
/*        btn4.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                move(Enum.Right);
            }
        });*/
        //简化之后
        //btn4.addActionListener((ActionEvent actionEvent)->{move(Enum.Right);});
        btn4.addActionListener((actionEvent)->{move(Enum.Right);});
    }
    public static void move(Enum a){
        switch(a){
            case UP:
                System.out.println("超级玛丽向上飞");
                break;
            case Down:
                System.out.println("超级玛丽向下飞");
                break;
            case Left:
                System.out.println("超级玛丽向左飞");
                break;
            case Right:
                System.out.println("超级玛丽向右飞");
                break;
            default:
                System.out.println("异常");
        }
    }
    enum  Enum {
        UP,Down,Left,Right;
    }

}
